﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using CefSharp;
using CefSharp.WinForms;
using Newtonsoft.Json.Linq;
using System.Threading;

namespace warehouse
{
    class UpdaterEvent
    {
        private static System.Diagnostics.Process p;
        private static log4net.ILog log = log4net.LogManager.GetLogger("UpdaterEvent");
        public Dictionary<string, IJavascriptCallback> listeners = new Dictionary<string, IJavascriptCallback>();
        private ChromiumWebBrowser chrome;

        public UpdaterEvent(ChromiumWebBrowser chrome)
        {
            this.chrome = chrome;
        }

        public void addEventListener(string listenerName, IJavascriptCallback callBackParam)
        {
            //CheckComplete
            listeners[listenerName]= callBackParam;
        }
        public  void check(string param)
        {
            updater.LoopCallbackHandler callback = (string data, string code, string result) => {
                IJavascriptCallback callBack = listeners["CheckComplete"];
                if ("check" == data)
                {
                    JObject o = (JObject)JsonConvert.DeserializeObject(result);
                    if (callBack != null)
                    {
                        ResponseEntity responseEntity = new ResponseEntity();
                        responseEntity.code = "check";
                        if("True"== o["update"].ToString())
                        {
                            responseEntity.result = "0";
                        }
                        else
                        {
                            responseEntity.result = "1";
                        }
                        callBack.ExecuteAsync(responseEntity);
                    }
                }
            };
            updater.Check(param, callback, "check");
        }
        public void download(string param)
        {
            log.Info("download====" + param);
            Thread thread = new Thread(()=> {
                updater.DownLoad(param, (string data, string code, string result) => {
                    Console.WriteLine(result + "====res==========");
                    JObject value = JObject.Parse(result);
                    if (value["success"] != null && "true".Equals(value["success"].ToString().ToLower()))
                    {
                        IJavascriptCallback callBack = listeners["DownloadComplete"];
                        if (callBack != null)
                        {
                            callBack.ExecuteAsync(result);
                        }
                    }
                    else if (value["file"] != null)
                    {
                        var file = value["file"].ToString();
                        int end = file.LastIndexOf("/");
                        file = file.Substring(end + 1);
                        var dtotal = value["dtotal"].ToString();
                        var dnow = value["dnow"].ToString();
                    }
                }, "download");
            });
            thread.Start();
           
        }

        public void update(string param)
        {
            log.Info("update:" + param);
            if (p == null)
            {
                p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "update.exe";
                p.StartInfo.Arguments = "update";
                p.Start();
            }
            else
            {
                if (p.HasExited) //是否正在运行
                {
                    p.Start();
                }
            }
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        }
        public void removeEventListener(string listenerName)
        {
            Console.WriteLine(listenerName + "=================");
            if (listeners.ContainsKey(listenerName))
            {
                listeners.Remove(listenerName);
            }
        }
    }
}
